package com.user_service.controller;

import com.user_service.bean.User;
import com.user_service.bean.UserDTO;
import com.user_service.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/internal/users")
@RequiredArgsConstructor
public class UserInternalController {

    private final UserRepository userRepository;

    @GetMapping("/{userId}")
    public UserDTO getUserById(@PathVariable Long userId) {
        return userRepository.findById(userId)
                .map(user -> {
                    UserDTO dto = new UserDTO();
                    dto.setId(user.getId());
                    dto.setUsername(user.getUsername());
                    dto.setEmail(user.getEmail());
                    return dto;
                })
                .orElseThrow(() -> new RuntimeException("用户不存在: " + userId));
    }

    @GetMapping("/{userId}/exists")
    public Boolean checkUserExists(@PathVariable Long userId) {
        return userRepository.existsById(userId);
    }
}
